package com.ashley.apptools.item;

import java.text.DecimalFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.ashley.apptools.R;
import com.ashley.apptools.interfaces.IActivity;
import com.ashley.apptools.provider.ToolsData.ItemNumber;
import com.ashley.apptools.utils.PriceCount;

public class ItemSaveConteol implements IActivity{
	private Activity mContext;
	private Spinner categorySpinner;
	private EditText itemEditText;
	private EditText cubeEditText;
	private EditText cubebByEditText;
	private EditText fobPriceEditText;
	private EditText freightEditText;
	private EditText taxEditText;
	private EditText landingEditText;
	private EditText warehovseEditText;
	private EditText saleEditText;
	private EditText retailEditText;
	private EditText profitEditText;
	private Button saveButton;
	public ItemSaveConteol(Activity activity) {
		// TODO Auto-generated constructor stub
		mContext = activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		categorySpinner = (Spinner) mContext.findViewById(R.id.category_sp);
		itemEditText =  (EditText) mContext.findViewById(R.id.item_no_edit);
		cubeEditText = (EditText) mContext.findViewById(R.id.cube_edit);
		cubebByEditText = (EditText) mContext.findViewById(R.id.cube_by);
		fobPriceEditText = (EditText) mContext.findViewById(R.id.fob_price);
		freightEditText = (EditText) mContext.findViewById(R.id.freight);
		taxEditText = (EditText) mContext.findViewById(R.id.tax_edit);
		landingEditText = (EditText) mContext.findViewById(R.id.landing_edit);
		warehovseEditText = (EditText) mContext.findViewById(R.id.warehovse_edit);
		saleEditText = (EditText) mContext.findViewById(R.id.sale_edit);
		retailEditText = (EditText) mContext.findViewById(R.id.retail_edit);
		profitEditText = (EditText) mContext.findViewById(R.id.profit_edit);
		saveButton = (Button) mContext.findViewById(R.id.save_but);
		
		
		
		fobPriceEditText.addTextChangedListener(textWatcher);
		cubebByEditText.addTextChangedListener(textWatcher);
		cubeEditText.addTextChangedListener(textWatcher);
		taxEditText.addTextChangedListener(textWatcher);;
		landingEditText.addTextChangedListener(textWatcher);;
		warehovseEditText.addTextChangedListener(textWatcher);
		saleEditText.addTextChangedListener(textWatcher);
		saveButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				save();
			}
		});
		
		fobPriceEditText.setOnFocusChangeListener(changeListener);
		cubebByEditText.setOnFocusChangeListener(changeListener);
		cubeEditText.setOnFocusChangeListener(changeListener);
		taxEditText.setOnFocusChangeListener(changeListener);
		landingEditText.setOnFocusChangeListener(changeListener);
		warehovseEditText.setOnFocusChangeListener(changeListener);
		saleEditText.setOnFocusChangeListener(changeListener);
	}


	private View.OnFocusChangeListener changeListener = new View.OnFocusChangeListener() {
		
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			if (hasFocus) {
				EditText view = (EditText) v;
				view.setSelectAllOnFocus(true);
			}
			
		}
	};
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRestart() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
	}
	
	private Handler handler = new Handler()
	{
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				freightEditText.setText(msg.obj.toString());
				break;
			case 1:
				
				retailEditText.setText(new DecimalFormat("0.00").format(msg.obj));
				
				break;
			case 2:
				 double r= (Double) msg.obj;
				 r = r *100;
				String o = new DecimalFormat("0.00").format(r);
				profitEditText.setText(o +"%");
				break;
			}
		};
	};
	
	@SuppressLint("DefaultLocale")
	private void save()
	{
		if(itemEditText.getText().length() < 2)
		{
			Toast.makeText(mContext.getApplicationContext(), "Please enter the item No.", Toast.LENGTH_LONG).show();
		}else if(checkInput())
		{
			ContentValues contentValues = new ContentValues();
			
			contentValues.put(ItemNumber.COLUME_ITEM_NO, itemEditText.getText().toString().toUpperCase());
			contentValues.put(ItemNumber.COLUME_CATEGORY, categorySpinner.getSelectedItemId());
			contentValues.put(ItemNumber.COLUME_FREIGHT, freightEditText.getText().toString());
			contentValues.put(ItemNumber.COLUME_CUBE, cubeEditText.getText().toString());
			contentValues.put(ItemNumber.COLUME_PRICE, fobPriceEditText.getText().toString());
			contentValues.put(ItemNumber.COLUME_TAX, taxEditText.getText().toString());
			contentValues.put(ItemNumber.COLUME_LANDING, landingEditText.getText().toString());
			contentValues.put(ItemNumber.COLUME_WAREHOVSE, warehovseEditText.getText().toString());
			contentValues.put(ItemNumber.COLUME_SALE, saleEditText.getText().toString());
			contentValues.put(ItemNumber.CLUME_RETAIL, retailEditText.getText().toString());
			mContext.getContentResolver().insert(ItemNumber.CONTENT_URI, contentValues);
		}else
		{
			Toast.makeText(mContext.getApplicationContext(), "Please input the information", Toast.LENGTH_LONG).show();
		}
	}
	private boolean checkInput()
	{
		boolean bo = false;
		
		if(fobPriceEditText.getText().length() > 0 && cubebByEditText.getText().length() >0 &&cubeEditText.getText().length() >0 && taxEditText.getText().length() >0
					&& landingEditText.getText().length() >0 && warehovseEditText.getText().length() >0	&& saleEditText.getText().length() >0)
		{
			bo = true;
		}else
		{
			bo = false;
		}
		return bo;
	}
	private TextWatcher textWatcher = new TextWatcher() {
		
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
			
			
			
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			if(checkInput())
			{
				compute();
			}else if(cubeEditText.getText().length() >0 && cubebByEditText.getText().length() >0)
			{
				freightCount();
			}
		}
	};
	private float freightCount()
	{
		float cubes = Float.parseFloat(cubeEditText.getText().toString());
		float cubeBy = Float.parseFloat(cubebByEditText.getText().toString());
		float freight = PriceCount.freightCount(cubes, cubeBy);
		handler.obtainMessage(0, freight).sendToTarget();
		return freight;
	};
	private void compute()
	{
		float fobPrice = Float.parseFloat(fobPriceEditText.getText().toString());
		
		float tax = Float.parseFloat(taxEditText.getText().toString());
		float landing = Float.parseFloat(landingEditText.getText().toString());
		float warehovse = Float.parseFloat(warehovseEditText.getText().toString());
		float sale = Float.parseFloat(saleEditText.getText().toString());
		
		float freight = freightCount();
		
		float tax_count = PriceCount.taxCount(fobPrice, tax);
		
		float landing_Price = PriceCount.landingPriceCount(fobPrice, freight, tax_count);
		
		
		
		float landing_count = PriceCount.landingProfitCount(landing, landing_Price);
		float warehovse_count = PriceCount.warehovseProfitCount(landing_count, warehovse);
		
		float sale_count = PriceCount.saleProfitCount(warehovse_count, sale);
		
		
		double retail_price = PriceCount.retailPrice(sale_count);
		
		double overall_profit = PriceCount.overallProfit(retail_price, landing_Price);
		
		handler.obtainMessage(1, retail_price).sendToTarget();
		handler.obtainMessage(2, overall_profit).sendToTarget();	
	}
}
